package outfox.smt.word.alignment;

import java.io.File;
import java.io.IOException;

import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class DocumentAlignmentBatch {

	public static void alignment(File folder) throws IOException{
		if(!folder.isDirectory())
			return;
		File[]files=folder.listFiles();
		String cpath=null;
		String epath=null;
		String fpath=null;
		String rpath=null;
		String spath=null;
		for(int i=0;i<files.length;i++){
			if(files[i].getName().equalsIgnoreCase("c.docx")){
				cpath=files[i].getPath();
			}
			if(files[i].getName().equalsIgnoreCase("e.docx")){
				epath=files[i].getPath();
			}
			if(files[i].getName().equalsIgnoreCase("f.docx")){
				fpath=files[i].getPath();
			}
			if(files[i].getName().equalsIgnoreCase("r.docx")){
				rpath=files[i].getPath();
			}
			if(files[i].getName().equalsIgnoreCase("s.docx")){
				spath=files[i].getPath();
			}
		}
		if(cpath==null){
			return;
		}
		DocumentAlignment da=new DocumentAlignment();
		XWPFDocument cdoc=null;
		String outpath;
		
		if(epath!=null){
			outpath=folder.getPath()+File.separator+"ce.txt";
			File file=new File(outpath);
			if(!file.exists()){
				System.out.println("Starting Document Alignment:"+cpath+"||"+epath);
				XWPFDocument edoc=XWPFDocument.newDocument(epath);
				if(cdoc==null)
					cdoc=XWPFDocument.newDocument(cpath);
				da.alignment(cdoc, edoc, outpath);
				edoc=null;
			}
		}
		
		if(fpath!=null){
			outpath=folder.getPath()+File.separator+"cf.txt";
			File file=new File(outpath);
			if(!file.exists()){
				System.out.println("Starting Document Alignment:"+cpath+"||"+fpath);
				XWPFDocument fdoc=XWPFDocument.newDocument(fpath);
				if(cdoc==null)
					cdoc=XWPFDocument.newDocument(cpath);
				da.alignment(cdoc, fdoc, outpath);
				fdoc=null;
			}
		}
		
		if(rpath!=null){
			outpath=folder.getPath()+File.separator+"cr.txt";
			File file=new File(outpath);
			if(!file.exists()){
				System.out.println("Starting Document Alignment:"+cpath+"||"+rpath);
				XWPFDocument rdoc=XWPFDocument.newDocument(rpath);
				if(cdoc==null)
					cdoc=XWPFDocument.newDocument(cpath);
				da.alignment(cdoc, rdoc, outpath);
				rdoc=null;
			}
		}
		
		if(spath!=null){
			outpath=folder.getPath()+File.separator+"cs.txt";
			File file=new File(outpath);
			if(!file.exists()){
				System.out.println("Starting Document Alignment:"+cpath+"||"+spath);
				XWPFDocument sdoc=XWPFDocument.newDocument(spath);
				if(cdoc==null)
					cdoc=XWPFDocument.newDocument(cpath);
				da.alignment(cdoc, sdoc, outpath);
				sdoc=null;
			}
		}
		cdoc=null;
	}
	
	public static void alignmentOfYear(File yearfolder){
		for(File tendayfolder:yearfolder.listFiles()){
			for(File folder:tendayfolder.listFiles()){
				try {
					alignment(folder);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) throws IOException {
		//alignment(new File("E:\\eclipse\\workspace\\poitest\\test\\2009.11.7-2009.11.16\\2"));
//		File f=new File("test/2009.11.7-2009.11.16");
//		File[]folders=f.listFiles();
		long start_time=System.currentTimeMillis();
//		for(File folder:folders){
//			try{
//				alignmentOfYear(new File("D:\2001"));
//			}catch(Exception e){
//				e.printStackTrace();
//			}
//		}
		alignmentOfYear(new File("f:\\2001\\docs"));
		long end_time=System.currentTimeMillis();
		System.out.println("--------");
		System.out.println("Total Time:"+(end_time-start_time)/1000+"s");
		System.out.println("CE Sentences Alignment:"+DocumentAlignment.cecount);
		System.out.println("CF Sentences Alignment:"+DocumentAlignment.cfcount);
		System.out.println("CR Sentences Alignment:"+DocumentAlignment.crcount);
		System.out.println("CS Sentences Alignment:"+DocumentAlignment.cscount);
	}
}